package com.mp.chart.markerview;

import android.content.Context;
import android.widget.TextView;


import com.android.util.log.LogUtil;
import com.mp.chart.components.MarkerView;
import com.mp.chart.data.Entry;
import com.mp.chart.data.KLineData;
import com.mp.chart.data.MinutesData;
import com.mp.chart.utils.PriceColorUtil;
import com.mp.chart.utils.StringUtils;
import com.mp.chart.utils.VolumeUtils;
import com.android.R;

import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * 长按时的框
 * Created by TenFace on 2017/9/26.
 */

public class QuoteInfoMarkerView extends MarkerView {
    /**
     * Constructor. Sets up the MarkerView with a custom layout resource.
     *
     * @param context
     * @param layoutResource the layout resource to use for the MarkerView
     */
    private final boolean isOut;
    private DecimalFormat mFormat;
    private TextView tvContent;
    private String type;//com.mp.chart type
    private TextView markerTv1;
    private TextView markerTv2;
    private TextView markerTv3;
    private TextView markerTv4;
    private TextView markerTv5;
    private TextView markerTv6;
    private TextView markerText1;
    private TextView markerText2;
    private TextView markerText3;
    private TextView markerText4;
    private TextView markerText5;
    private TextView markerText6;
    private String num;

    public QuoteInfoMarkerView(String type, Context context, int layoutResource, boolean isOut) {
        super(context, layoutResource);
        this.type = type;
        this.isOut = isOut;
        markerTv1 = (TextView) findViewById(R.id.markerTv1);
        markerTv2 = (TextView) findViewById(R.id.markerTv2);
        markerTv3 = (TextView) findViewById(R.id.markerTv3);
        markerTv4 = (TextView) findViewById(R.id.markerTv4);
        markerTv5 = (TextView) findViewById(R.id.markerTv5);
        markerTv6 = (TextView) findViewById(R.id.markerTv6);
        markerText1 = (TextView) findViewById(R.id.markerText1);
        markerText2 = (TextView) findViewById(R.id.markerText2);
        markerText3 = (TextView) findViewById(R.id.markerText3);
        markerText4 = (TextView) findViewById(R.id.markerText4);
        markerText5 = (TextView) findViewById(R.id.markerText5);
        markerText6 = (TextView) findViewById(R.id.markerText6);
        markerTv1.setTextColor(getResources().getColor(R.color.main_white));
    }

    @Override
    public void refreshContent(Entry e, int dataSetIndex) {
        if ("kline".equals(type)) {
            KLineData data = (KLineData) e.getData();
            if (data != null) {
                markerText1.setText("Time");
                markerText2.setText("Open");
                markerText3.setText("High");
                markerText4.setText("Low");
                markerText5.setText("Close");
                markerText6.setText("Percent");
                markerText6.setVisibility(VISIBLE);

                markerTv1.setText(data.date);
//                markerTv2.setText(String.valueOf(new BigDecimal(data.open).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue()));
//                markerTv3.setText(String.valueOf(new BigDecimal(data.high).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue()));
//                markerTv4.setText(String.valueOf(new BigDecimal(data.low).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue()));
//                markerTv5.setText(String.valueOf(new BigDecimal(data.close).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue()));
//                LogUtil.e(data.lastClose);
                PriceColorUtil.setPriceColor(getContext(), markerTv2, new BigDecimal(data.open).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue(), new BigDecimal(data.lastClose).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue());
                PriceColorUtil.setPriceColor(getContext(), markerTv3, new BigDecimal(data.high).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue(), new BigDecimal(data.lastClose).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue());
                PriceColorUtil.setPriceColor(getContext(), markerTv4, new BigDecimal(data.low).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue(), new BigDecimal(data.lastClose).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue());
                PriceColorUtil.setPriceColor(getContext(), markerTv5, new BigDecimal(data.close).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue(), new BigDecimal(data.lastClose).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue());

                markerTv6.setVisibility(VISIBLE);
                markerTv6.setText("" + StringUtils.doubleToDoubleString(data.percent) + "%");
                markerTv6.setTextColor(PriceColorUtil.getPriceColor(getContext(), new BigDecimal(data.close).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue(), new BigDecimal(data.lastClose).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue()));
            } else {
                markerTv1.setText("--");
                markerTv2.setText("--");
                markerTv3.setText("--");
                markerTv4.setText("--");
                markerTv5.setText("--");
                markerTv6.setText("--");
                markerText6.setVisibility(GONE);
                markerTv6.setVisibility(GONE);
            }
        } else if ("timeline".equals(type)) {
            MinutesData data = (MinutesData) e.getData();
            if (data != null) {
                markerText1.setText("时间");
                markerText2.setText("价格");
                markerText3.setText("均价");
                markerText4.setText("涨跌幅");
                markerText5.setText("成交量");
                markerText6.setVisibility(GONE);

                markerTv1.setText(data.time);
                PriceColorUtil.setPriceColor(getContext(), markerTv2, data.chengjiaojia, data.lastClose);

                PriceColorUtil.setPriceColor(getContext(), markerTv3, data.junjia, data.lastClose);
                markerTv4.setText("" + StringUtils.doubleToDoubleString(data.percentage) + "%");
                markerTv4.setTextColor(PriceColorUtil.getPriceColor(getContext(), data.chengjiaojia, data.lastClose));
                markerTv5.setText(VolumeUtils.getVolUnit(data.chengjiaoliang));
                markerTv6.setVisibility(GONE);
            } else {
                markerTv1.setText("--");
                markerTv2.setText("--");
                markerTv3.setText("--");
                markerTv4.setText("--");
                markerTv5.setText("--");

                markerText6.setVisibility(GONE);
                markerTv6.setVisibility(GONE);
            }
        }
    }

    @Override
    public int getXOffset() {
        return 0;
    }

    @Override
    public int getYOffset() {
        return 0;
    }

}
